<?php
require_once 'ApibaseController.php';

/**
 * 各種問い合わせAPI<br />
 *
 * 各種問い合わせAPI
 *
 * @package apamanshop
 * @author ushijima
 * @copyright コピーライト(要確認)
 */
class Inquiry_ApietcController extends Inquiry_ApibaseController
{
	/**
	 * 各種問合せAPIアクション
	 *
	 * @access public
	 * @return void
	 */
	public function indexAction()
	{

		// 変数初期化処理 start
		$strCtgryCd = $this->_params['CtgryCd'];				//ｶﾃｺﾞﾘ区分
		$strSiteKbn = $this->_params['SiteKbn'];				//ｻｲﾄ区分
		$strPlaceKbn = $this->_params['PlaceKbn'];			//地域ｻｲﾄID

		$strName = $this->_params['Name'];					//名前
		$strNameKana = $this->_params['NameKana'];			//名前カナ 20141211 Add
		$strTel = $this->_params['Tel'];						//TEL
		$strMail = $this->_params['Mail'];					//ﾒｰﾙ
		$strTargetKbn = $this->_params['TargetKbn'];			//対象区分
		$strTargetNm = $this->_params['TargetNm'];			//対象区分名称
		$strClaimTgtKbn = $this->_params['ClaimTgtKbn'];		//ｸﾚｰﾑ対象区分
		$strShopCd = $this->_params['ShopCd'];				//店舗CD
		$strShopNm = $this->_params['ShopNm'];				//店舗名称
		$strTenpoNm = $this->_params['TenpoNm'];				//店舗名称
		$strCharge = $this->_params['Charge'];				//担当者
		$strComment = $this->_params['Comment'];				//ｺﾒﾝﾄ

		$strAge = $this->_params['AgeKbn'];					//年齢
		$strSex = $this->_params['Sex'];						//性別
		$strMarriage = $this->_params['Mrrg'];				//結婚
		$strJob = $this->_params['JobKbn'];					//職業
		$strYuubin1 = $this->_params['Zip1'];				//郵便番号1
		$strYuubin2 = $this->_params['Zip2'];				//郵便番号2
		$strPrefCd = $this->_params['PrefCd'];				//都道府県CD
		$strPrefName = $this->_params['PrefName'];			//都道府県テキスト
		$strAddress = $this->_params['Address'];				//住所
		$strFax = $this->_params['Fax'];						//FAX

		//送信時刻
		$strTimeStr = $this->_params['TimeStr'];

		//固定設定になってた
		$this->_params['CtgryCd'] = "01";

		$add_params['CtgyNm'] = "お問い合わせ";
		$add_params['Title'] = "お客様からのご意見（お問い合わせ）を受付けました";
		$add_params['TitleMail'] = "ご意見メール(お問い合わせ)";
		$add_params['HTMLTitle'] = "賃貸情報アパマンショップ[各種お問い合わせ│お問い合わせ]";
		$add_params['SubTitle'] = "各種お問い合わせ　&raquo;　送信完了";

		$add_params['H1'] = "賃貸住宅・賃貸マンション・賃貸アパートの検索ならアパマンショップ。全国の賃貸住宅、賃貸不動産情報が満載です。";

		$add_params['MAIL_NETWORK'] = Apm_Config_Mail::MAIL_NETWORK;

		//--- メール判定 ---
		$this->setSendMailFlag($this->_params['Mail']);

		//各種設定処理
		//ｸﾚｰﾑ対象区分が1(=本部)である場合
		if ($strClaimTgtKbn == "1"){

			$this->_params['ClaimTgtNm'] = "アパマンショップ本部";
			//$this->_params['Comment'] = "対象:" . $strClaimTgtNm . "\r\n" . "\r\n" . "　".  $strComment;
			$add_params['UserCd'] = Apm_Config_Inquiry::TAIOU_USR_CD_HONBU;
			$add_params['ClaimTgtKbnIcon'] = "09";
			$add_params['UserSonotaCd'] = "NULL";

		//ｸﾚｰﾑ対象区分が2(=店舗)である場合
		} else if ($strClaimTgtKbn == "2") {

			$this->_params['ClaimTgtNm'] = "店舗";
			//$this->_params['Comment'] = "対象:" . $strClaimTgtNm .  "\r\n" . "\r\n" . "　". $strComment;
			$add_params['UserCd'] = $strShopCd;
			$add_params['ClaimTgtKbnIcon'] = "03";
			$add_params['UserSonotaCd'] = "NULL";

			//担当者が設定されていない場合
			if ( trim($this->_params['Charge']) == "") {
				 //$this->_params['Comment'] = "対象店舗:" . $strTenpoNm .  "\r\n" . "\r\n" . "　". $strComment;
			//担当者が設定されている場合
			} else {
				 //$this->_params['Comment'] = "対象店舗:" . $strTenpoNm . "　対象担当:" . trim($this->_params['Charge']) . "\r\n" . "\r\n" . "　". $strComment;
			}

		//ｸﾚｰﾑ対象区分が3(=その他)である場合
		} else if ($strClaimTgtKbn == "3") {

			$this->_params['ClaimTgtNm'] = "その他";
			//$this->_params['Comment'] = "対象:". $strClaimTgtNm .  "\r\n" . "\r\n" . "　". $strComment;
			$add_params['UserCd'] = "";
			$add_params['ClaimTgtKbnIcon'] = "99";
			$add_params['UserSonotaCd'] = "'" . Apm_Config_Inquiry::TAIOU_USR_CD_SONOTA . "'";

		}

		//名前設定
		if (trim($this->_params['Name']) == "") {
			$this->_params['Name'] = "無記名";
		}

		//電話番号設定
		if (trim($this->_params['Tel']) == "") {
//			$this->_params['Tel'] = "NULL";
		}else{
			$this->_params['Tel'] =  $this->_params['Tel'];
		}

		//備考欄
//		$add_params['Bikou'] =  "'" . $this->_params['PrefName'] . " " . trim($this->_params['Address']) . "'";// 20141211 CommentOut
		$add_params['Bikou'] = "'" .trim($this->_params['NameKana'] . "'");// 20141211 Add
		
		// ﾛｸﾞ情報格納処理 *****************************************

		try {
			// モデルの呼び出し
			$this->_model = new Inquiry_Model_Apiinquiry();

			//トランザクション開始
			$db = $this->_model->getDbConnection();

			//トランザクション開始
			$this->_model->startTransaction($db);

			if ($this->isLogIns()) {
					//対応状況TBLﾃﾞｰﾀ挿入処理
					$resApi = $this->_model->insTaiou($db,$this->_params,$add_params);
			}

			// ﾒｰﾙ送信処理 ********************************************
			$result =  $this->SendMailMain($strMail,$add_params);

			if($result == "0"){
				 $this->raiseError('メール送信エラー');
			}
			__log_debug(__FILE__, __LINE__, __METHOD__, "コミットします");
			$this->_model->commitTransaction($db);
			$db->closeConnection();

			$result = 1;
		} catch (Exception $e) {
			__log_debug(__FILE__, __LINE__, __METHOD__, "ロールバックします");
			$this->_model->rollbackTransaction($db);
			$db->closeConnection();
			$result = 0;
		}

		// 返却データ作成
		$resResult = array('api_result' => array('result' => $result,'res_detail' => $resApi));
		// 返却処理
		$this->_api_data = $resResult;
		$this->display();

	}

	//メール送信制御
	private function SendMailMain($mail,$addinfo) {
		// ｽﾃｰﾀｽ初期化

		// ユーザへメール送信
		if ($mail != "") {
			$userResult = $this->SendMailUser($mail,$addinfo);
		}

		// 本部へのメール送信
		$officeResult = $this->SendMailOffice($addinfo);

		$result = 1;
		if ( $userResult == 0 || $officeResult == 0) {
			$result = 0;
		}
		return $result;

	}

	// 問い合わせユーザへのメール送信
	private function SendMailUser($mail,$addinfo) {

		// from(送信元)、to（あて先)
		$strMailTo = array($mail => "");							//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array(Apm_Config_Mail::MAIL_NETWORK => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ

		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "［アパマンショップ］お問い合わせのご確認";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if ($this->isSendUser()) {
			$result = $this->sendToiawaseMail("MailEtcUser.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}
		return $result;
	}

	// 本部のメール送信
	private function SendMailOffice($addinfo) {

		$strSubject = $addinfo['Title'];
		$strMailTo = array(Apm_Config_Mail::MAIL_NETWORK => "");
		$strMailCc = array(Apm_Config_Mail::MAIL_WEBMASTER => "");
		$strMailFrom = array(Apm_Config_Mail::MAIL_NETWORK => "");			//送信元ﾒｰﾙｱﾄﾞﾚｽ

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = $strSubject;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = $strMailCc;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if($this->isSendHonbu() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailEtcHonbu.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceHonbuMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = array($this->getReplaceHonbuMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailEtcHonbu.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;

	}
}
?>
